import 'package:flutter/material.dart';
import 'package:zhaoxiaowu_app/components/view.dart';
import 'package:zhaoxiaowu_app/utils/alert_utils.dart';
import 'package:provider/provider.dart';
import 'package:zhaoxiaowu_app/utils/event_bus.dart';
import 'package:zhaoxiaowu_app/viewmodels/accounting_add_viewmodel.dart';
import 'package:zhaoxiaowu_app/viewmodels/accounting_viewmodel.dart';

class AccountingAddView extends StatefulWidget {
  const AccountingAddView({Key? key}) : super(key: key);

  @override
  _AccountingAddViewState createState() => _AccountingAddViewState();
}

class _AccountingAddViewState extends State<AccountingAddView> {
  late TextEditingController _money;
  late TextEditingController _desc;

  @override
  void initState() {
    _money = TextEditingController();
    _desc = TextEditingController();
    bus.on("accouting_add_result", (arg) {
      context.read<AccountingViewModel>().getAccountingHistory();
      setState(() {
        _money.text = "";
        _desc.text = "";
      });
    });
    super.initState();
  }

  @override
  void dispose() {
    _money.dispose();
    _desc.dispose();
    bus.off("accouting_add_result");
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: getAppBar("新增记账"),
      body: Padding(
        padding: EdgeInsets.all(10),
        child: Column(
          children: [
            ListTile(
              title: Text("类型"),
              trailing: Text(Provider.of<AccountingAddViewModel>(context).typeMap["name"]),
              onTap: _showTypesAlert,
            ),
            Divider(height: 1.0),
            ListTile(
              title: Text("方式"),
              trailing: Text(Provider.of<AccountingAddViewModel>(context).modeText),
              onTap: _accountingType,
            ),
            Divider(height: 1.0),
            Container(
              padding: EdgeInsets.all(10),
              child: TextField(
                decoration: InputDecoration(
                  border: InputBorder.none,
                  hintText: "请输入金额",
                ),
                keyboardType: TextInputType.number,
                textInputAction: TextInputAction.next,
                autofocus: true,
                controller: _money,
              ),
            ),
            Divider(height: 1.0),
            Container(
              padding: EdgeInsets.all(10),
              child: TextField(
                decoration: InputDecoration(
                  border: InputBorder.none,
                  hintText: "请输入备注",
                ),
                textInputAction: TextInputAction.next,
                controller: _desc,
              ),
            ),
            Divider(height: 1.0),
            SizedBox(height: 16),
            Container(
              width: double.infinity,
              height: 50.0,
              margin: EdgeInsets.symmetric(horizontal: 10),
              child: ElevatedButton(
                child: Text("新增"),
                onPressed: _submit,
              ),
            )
          ],
        ),
      ),
    );
  }

  void _showTypesAlert() async {
    var result = await showObjectAlertDialog(
      context.read<AccountingAddViewModel>().types,
      "选择类型",
      "name",
    );

    if (result != null && result != context.read<AccountingAddViewModel>().type) {
      context.read<AccountingAddViewModel>().setType(result);
    }
  }

  void _accountingType() async {
    context.read<AccountingAddViewModel>().getModeAndAlert();
  }

  void _submit() {
    context.read<AccountingAddViewModel>().insert(_money.text, _desc.text);
  }
}
